package com.beyond.market.evaluate.controller;

import com.beyond.market.evaluate.domain.Evaluate;
import com.beyond.market.evaluate.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/evaluate")
public class EvaluateController {
    @Autowired
    private EvaluateService evaluateService;

    // @RequestMapping(value = "/view/**", method = RequestMethod.GET)
    // public String html(HttpServletRequest request) {
    //     return StrUtil.removePrefix(request.getRequestURI(), request.getContextPath() + "/view");
    // }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/write")
    public String write() {
        return "write";
    }

    @RequestMapping("/save")
    @ResponseBody
    public int save(Evaluate evaluate) throws SQLException {
        int save = evaluateService.save(evaluate);
        System.out.println(save);
        return save;
    }

    @RequestMapping("/detail")
    public String detail() {
        return "detail";
    }

    @RequestMapping("/searchFor")
    public String searchFor() {
        return "searchFor";
    }

    @RequestMapping("/remove/{evaluateId}")
    @ResponseBody
    public int remove(@PathVariable("evaluateId") Integer evaluateId) throws SQLException {
        int remove = evaluateService.remove(evaluateId);
        return remove;
    }

    @RequestMapping("/update")
    @ResponseBody
    public int update(Evaluate evaluate) throws SQLException {
        int update = evaluateService.update(evaluate);
        return update;
    }

    @RequestMapping("/getByPrimaryKey")
        public String getByPrimaryKey(@RequestParam("ecaluateId") Integer evaluateId, Model model) throws SQLException {
        Evaluate evaluate = evaluateService.getByPrimaryKey(evaluateId);
        model.addAttribute("evaluate",evaluate);
        return "getByEvaluatedId";
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public List<Evaluate> listAll() throws SQLException {
        List<Evaluate> evaluate = evaluateService.listAll();
        return evaluate;
    }

    @RequestMapping("/getByUser/{id}")
    @ResponseBody
    public List<Evaluate> getByUser(@PathVariable("id") Integer userId) throws SQLException {
        List<Evaluate> list = evaluateService.getByUser(userId);
        return list;
    }

}

